/* Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
        limitations under the License.
==============================================================================*/

#include <fcntl.h>
#include <fstream>
#include <sys/stat.h>
#include <unistd.h>

#include "common.h"

namespace AclnnLazyAdam {
    bool ReadFile(const std::string &filePath, size_t fileSize, void *buffer, size_t bufferSize)
    {
        struct stat sBuf;
        int fileStatus = stat(filePath.data(), &sBuf);
        if (fileStatus == -1) {
            ERROR_LOG("failed to get file %s", filePath.c_str());
            return false;
        }
        if (S_ISREG(sBuf.st_mode) == 0) {
            ERROR_LOG("%s is not a file, please enter a file", filePath.c_str());
            return false;
        }

        std::ifstream file;
        file.open(filePath, std::ios::binary);
        if (!file.is_open()) {
            ERROR_LOG("Open file failed. path = %s", filePath.c_str());
            return false;
        }

        std::filebuf *buf = file.rdbuf();
        size_t size = buf->pubseekoff(0, std::ios::end, std::ios::in);
        if (size == 0) {
            ERROR_LOG("file size is 0");
            file.close();
            return false;
        }
        if (size > bufferSize) {
            ERROR_LOG("file size is larger than buffer size");
            file.close();
            return false;
        }
        buf->pubseekpos(0, std::ios::in);
        buf->sgetn(static_cast<char *>(buffer), size);
        fileSize = size;
        file.close();
        return true;
    }

    bool WriteFile(const std::string &filePath, const void *buffer, size_t size)
    {
        if (buffer == nullptr) {
            ERROR_LOG("Write file failed. buffer is nullptr");
            return false;
        }
        int fd = open(filePath.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWRITE);
        if (fd < 0) {
            ERROR_LOG("Open file failed. path = %s", filePath.c_str());
            return false;
        }

        auto writeSize = write(fd, buffer, size);
        (void) close(fd);
        if (writeSize != size) {
            ERROR_LOG("Write file Failed.");
            return false;
        }

        return true;
    }
}